package com.ctshk.rpc.goods.mapper;

import com.ctshk.rpc.goods.base.CustomBaseMapper;
import com.ctshk.rpc.goods.entity.CommodityCost;
import com.ctshk.rpc.goods.req.conmodity.CommodityCostReq;
import com.ctshk.rpc.goods.req.conmodity.CommodityCostSingleReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 商品成本表 Mapper 接口
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-13
 */
public interface CommodityCostMapper extends CustomBaseMapper<CommodityCost> {

    /**
     * 根据商品id获取商品成本信息
     * @param commodityId
     * @param commodityAttributeId
     * @param commodityCostAttributeIds
     * @return
     */
    @Select({
            "<script>",
            "SELECT",
            "t.id,t.commodity_attribute_id,t.commodity_id,t.cost_attribute_id,t.cost_price,t.price,t.marking_price,t.settlement_price,t.stock,t.weight,t.taxation,t.number,t.profit,t.gmt_create,t.create_id,t.gmt_modified,t.modified_id,t.is_deleted  ",
            "FROM `ctshk-goods`.t_commodity_cost t ",
            "WHERE t.is_deleted = 0 ",
            "<if test='commodityId != null'> AND t.commodity_id = #{commodityId}</if> ",
            "<if test='commodityAttributeId != null'> AND t.commodity_attribute_id = #{commodityAttributeId}</if> ",
            "<if test='commodityCostAttributeIds != null and commodityCostAttributeIds.size != 0'>",
            "AND t.cost_attribute_id IN ",
            "<foreach collection='commodityCostAttributeIds' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</if>",
            "</script>"
    })
    List<CommodityCost> findCommodityCostByCombinedIds(@Param("commodityId") Long commodityId, @Param("commodityAttributeId") Long commodityAttributeId,
                                                       @Param("commodityCostAttributeIds") List<Long> commodityCostAttributeIds);

    @Select("SELECT t.* FROM t_commodity_cost t WHERE t.commodity_id = #{commodityId} AND t.is_deleted = 0")
    List<CommodityCost> findCommodityCostListByCommodityId(@Param("commodityId") Long commodityId);

    @Select({
            "<script>",
            "SELECT ",
            "t.id, t.commodity_attribute_id, t.commodity_id, t.cost_attribute_id, t.cost_price, t.picture_url, t.profit, t.price, t.marking_price, t.settlement_price, t.stock, t.weight, t.taxation, t.number ",
            ", ca.id AS \"commodityCostAttributeReq.id\",  ca.commodity_attribute_id AS \"commodityCostAttributeReq.commodity_attribute_id\",  ca.commodity_id AS \"commodityCostAttributeReq.commodity_id\",  ca.specifications_attribute_one_id AS \"commodityCostAttributeReq.specifications_attribute_one_id\",  ca.specifications_attribute_one_name AS \"commodityCostAttributeReq.specifications_attribute_one_name\",  ca.specifications_attribute_two_id AS \"commodityCostAttributeReq.specifications_attribute_two_id\",  ca.specifications_attribute_two_name AS \"commodityCostAttributeReq.specifications_attribute_two_name\" ",
            "FROM `ctshk-goods`.t_commodity_cost t ",
            "LEFT JOIN `ctshk-goods`.t_commodity_cost_attribute ca ON ca.id = t.cost_attribute_id ",
            "WHERE t.is_deleted = 0 ",
            "<if test='commodityId != null'> AND t.commodity_id = #{commodityId}</if> ",
            "<if test='commodityAttributeId != null'> AND t.commodity_attribute_id = #{commodityAttributeId}</if> ",
            "</script>"
    })
    List<CommodityCostSingleReq> findCommodityCostAndAttributeByCombinedIds(@Param("commodityId") Long commodityId, @Param("commodityAttributeId") Long commodityAttributeId);

    @Select({
            "<script>",
            "SELECT ",
            "t.id,t.commodity_attribute_id,t.commodity_id,t.cost_attribute_id,t.picture_url,MIN(t.price) as \"price\",t.marking_price",
            "FROM t_commodity_cost t ",
            "WHERE t.is_deleted = 0 ",
            "<if test='commodityId != null'> AND t.commodity_id = #{commodityId}</if> ",
            "<if test='commodityAttributeId != null'> AND t.commodity_attribute_id = #{commodityAttributeId}</if> ",
            "<if test='specificationsType != null and specificationsType == 1'> AND t.cost_attribute_id IS NULL</if> ",
            "<if test='specificationsType != null and specificationsType == 2'> AND t.cost_attribute_id IS NOT NULL</if> ",
            "</script>"
    })
    CommodityCostReq findCommodityCostReqMinPriceByCombinedIds(@Param("commodityId") Long commodityId,
        @Param("commodityAttributeId") Long commodityAttributeId, @Param("specificationsType") Integer specificationsType);


    // =============================== H5端 Start ===============================
    @Select({
            "<script>",
            "SELECT",
            "t.id,t.commodity_attribute_id,t.commodity_id,t.cost_attribute_id,t.cost_price, t.gmt_create,t.create_id,t.gmt_modified,t.modified_id,t.is_deleted  ",
            "FROM t_commodity_cost t ",
            "WHERE t.is_deleted = 0 ",
            "<if test='commodityId != null'> AND t.commodity_id = #{commodityId}</if> ",
            "<if test='commodityAttributeId != null'> AND t.commodity_attribute_id = #{commodityAttributeId}</if> ",
            "<if test='commodityCostAttributeIds != null and commodityCostAttributeIds.size != 0'>",
            "AND t.cost_attribute_id IN ",
            "<foreach collection='commodityCostAttributeIds' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</if>",
            "</script>"
    })
    List<CommodityCost> findHtmlFiveCommodityCostByCombinedIds(@Param("commodityId") Long commodityId, @Param("commodityAttributeId") Long commodityAttributeId,
                                                               @Param("commodityCostAttributeIds") List<Long> commodityCostAttributeIds);

    @Select({
            "<script>",
            "SELECT ",
            "t.id, t.commodity_attribute_id, t.commodity_id, t.cost_attribute_id, t.cost_price, t.picture_url, t.profit, t.price, t.marking_price, t.settlement_price, t.stock, t.weight, t.taxation, t.number ",
            ", ca.id AS \"commodityCostAttributeReq.id\",  ca.commodity_attribute_id AS \"commodityCostAttributeReq.commodity_attribute_id\",  ca.commodity_id AS \"commodityCostAttributeReq.commodity_id\",  ca.specifications_attribute_one_id AS \"commodityCostAttributeReq.specifications_attribute_one_id\",  ca.specifications_attribute_one_name AS \"commodityCostAttributeReq.specifications_attribute_one_name\",  ca.specifications_attribute_two_id AS \"commodityCostAttributeReq.specifications_attribute_two_id\",  ca.specifications_attribute_two_name AS \"commodityCostAttributeReq.specifications_attribute_two_name\" ",
            "FROM t_commodity_cost t ",
            "LEFT JOIN t_commodity_cost_attribute ca ON ca.id = t.cost_attribute_id ",
            "WHERE t.is_deleted = 0 ",
            "<if test='commodityId != null'> AND t.commodity_id = #{commodityId}</if> ",
            "<if test='commodityAttributeId != null'> AND t.commodity_attribute_id = #{commodityAttributeId}</if> ",
            "</script>"
    })
    List<CommodityCostSingleReq> findHtmlFiveCommodityCostAndAttributeByCombinedIds(@Param("commodityId") Long commodityId, @Param("commodityAttributeId") Long commodityAttributeId);

    // =============================== H5端 End ===============================

    @Select("SELECT t.id, t.cost_price, t.marking_price, t.price, t.picture_url, t.profit from t_commodity_cost t where t.commodity_attribute_id = #{commodityAttributeId}")
    CommodityCost selectCommodityCostByCommodityAttributeId(@Param("commodityAttributeId") Long commodityAttributeId);
}
